package com.github.yangyishe;

import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public static TreeNode getInstance(Integer[] arr){
        TreeNode[] treeNodes=new TreeNode[arr.length];
        for(int i=0;i<arr.length;i++){
            if(arr[i]!=null){
                treeNodes[i]=new TreeNode(arr[i]);
            }
        }

        for(int i=0;i<=treeNodes.length/2-1;i++){
            if(treeNodes[i]!=null&&i*2+1<treeNodes.length){
                treeNodes[i].left=treeNodes[i*2+1];
            }
            if(treeNodes[i]!=null&&i*2+2<treeNodes.length){
                treeNodes[i].right=treeNodes[i*2+2];
            }
        }


        return treeNodes[0];
    }

    public static TreeNode getInstanceNew(Integer[] arr){
        if(arr.length==0){
            return null;
        }
        List<List<TreeNode>> legalParentNodeList=new ArrayList<>();
        Iterator<Integer> iterator = Arrays.stream(arr).iterator();
        legalParentNodeList.add(Arrays.asList(new TreeNode(iterator.next())));
        int level=0;


        boolean iterateFinish=false;
        while(true){
            List<TreeNode> treeNodeList = legalParentNodeList.get(level);
            legalParentNodeList.add(new ArrayList<>());
            for(TreeNode treeNode:treeNodeList){

                if(treeNode!=null){
                    if(!iterator.hasNext()){
                        iterateFinish=true;
                        break;
                    }
                    Integer left = iterator.next();
                    if (left!=null) {
                        treeNode.left=new TreeNode(left);
                        legalParentNodeList.get(level+1).add(treeNode.left);
                    }
                    if(!iterator.hasNext()){
                        iterateFinish=true;
                        break;
                    }
                    Integer right = iterator.next();
                    if(right!=null){
                        treeNode.right=new TreeNode(right);
                        legalParentNodeList.get(level+1).add(treeNode.right);
                    }
                }

            }
            if(!iterateFinish){
                level++;
            }else{
                break;
            }

        }


        return legalParentNodeList.get(0).get(0);
    }

    public void preOrder(){
        System.out.println(this);
        if(this.left!=null){
            this.left.preOrder();
        }
        if(this.right!=null){
            this.right.preOrder();
        }
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "val=" + val +
                '}';
    }
}
